﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Planer.Model.Validators;

namespace Planer.Model
{
    public class FinancicalGoalModel
    {
        static FinancicalGoalModel model = null;

        protected FinancicalGoalModel()
        {            
        }

        public static FinancicalGoalModel GetInstance()
        {
            if (model == null)
            {
                model = new FinancicalGoalModel();
            }

            return model;
        }

        public List<financial_goal> GetFinancicalGoalListByUser(user User)
        {
            var query = from goal in Program.Db.financial_goal
                        where goal.user.id == User.id && goal.spent == "n"
                        orderby goal.create_date descending
                        select goal;
            return new List<financial_goal>(query.ToArray<financial_goal>());
        }

        public ModelResult SaveOrUpdateFinancicalGoal(financial_goal Goal)
        {
            FinancicalGoalValidator validator = new FinancicalGoalValidator();
            validator.Validate(Goal);
            if (validator.Status == OperationStatusType.ERROR)
            {
                return new ModelResult(validator.Errors);
            }

            if (Goal.id == 0)
                Program.Db.AddTofinancial_goal(Goal);
            Program.Db.SaveChanges();

            return new ModelResult();
        }
    }
}
